const Router = require('restify-router').Router;

const { RunService } = require('../service/runService');
const { WVResult } = require('../utils/WVResultUtil');

const router = new Router();

router.post('', (req, res, next) => {
  if (!(req && req.body && req.body.code)) {
    res.send(new WVResult(false, '缺少参数'));
    return next();
  }
  const code = req.body.code;
  const runService = new RunService();
  runService.runCode(code)
    .then((ret) => {
      res.send(new WVResult(ret.suc, ret.runInfo));
      return next();
    })
    .catch((err) => {
      res.send(new WVResult(false, err));
      return next();
    });
});

module.exports = router;
